//
//  CardView.swift
//  PlayGame
//
//  Created by woniu on 2025/8/13.
//

import Foundation
import UIKit

enum CardType: String {
    case icon = "图片"
    case txt = "文本"
    case none = ""
}

class CardView: UIView {
    var onSelectIconBlock: ((_ iconName:String)->())?
    var onSelectTextBlock: ((_ word:String)->())?
    public var isPlaySoundWhenTap = false
    
    private var bgImageView:UIImageView!
    private var numLab:UILabel!
    private var iconView:UIImageView!
    private var cardType:CardType = .none
    private var wordStr = ""
    
    override init(frame: CGRect){
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        self.layer.cornerRadius = 5
        self.layer.masksToBounds = true
        bgImageView = UIImageView()
        bgImageView.backgroundColor = .black
        bgImageView.contentMode = .scaleAspectFit
        bgImageView.image = UIImage(named: "bg_card_1")
        self.addSubview(bgImageView)
        
        numLab = UILabel()
        numLab.textAlignment = .center
        numLab.font = UIFont.boldSystemFont(ofSize:Utils.isPad() ? 30 : 17)
        numLab.numberOfLines = 0
        numLab.backgroundColor = .white
        numLab.textColor = UIColor.orange
        numLab.isHidden = true
        self.addSubview(numLab)
        
        iconView = UIImageView()
        iconView.isHidden = true
        iconView.backgroundColor = .white
        iconView.contentMode = .scaleAspectFit
        self.addSubview(iconView)
        
        self.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer(target: self, action: #selector(onTap(_:)))
        self.addGestureRecognizer(tap)
        
        self.borderSet(.white, 1)
        self.circular5()
    }
    
    public func reset(){
        numLab.isHidden = true
        iconView.isHidden = true
        UIView.transition(with: bgImageView,
                          duration: 0.5,
                          options: [.transitionFlipFromLeft, .curveEaseInOut],
                          animations: {
            self.bgImageView.image =  UIImage(named: "bg_card_1")
            self.bgImageView.backgroundColor = .black
        },
                          completion: nil)
    }
    
    public func show(){
        UIView.transition(with: bgImageView,
                          duration: 0.5,
                          options: [.transitionFlipFromRight, .curveEaseInOut],
                          animations: {
            self.bgImageView.image =  nil
            self.bgImageView.backgroundColor = .white
            
        }) { re in
            if self.cardType == .txt{
                self.numLab.isHidden = false
            }else if self.cardType == .icon{
                self.iconView.isHidden = false
            }
        }
    }
    
    @objc func onTap(_ deco:UITapGestureRecognizer){
        if self.isPlaySoundWhenTap{
            Utils.playCardSound()
        }
        UIView.transition(with: bgImageView,
                          duration: 0.5,
                          options: [.transitionFlipFromRight, .curveEaseInOut],
                          animations: {
            self.bgImageView.image =  nil
            self.bgImageView.backgroundColor = .white
            
        }) { re in
            if self.cardType == .txt{
                self.numLab.isHidden = false
                self.onSelectTextBlock?(self.wordStr)
            }else if self.cardType == .icon{
                self.iconView.isHidden = false
                self.onSelectIconBlock?(self.wordStr)
            }
        }
    }
    
    public func updateCard(bgImage:UIImage){
        bgImageView.image = bgImage
    }
    
    public func update(title:String){
        self.cardType = .txt
        self.wordStr = title
        numLab.text = title
        iconView.isHidden = true
    }
    
    public func update(wordItem:WordItem){
        self.cardType = .icon
        self.wordStr = wordItem.word
        numLab.text = wordStr
        if let icon = wordItem.image{
            iconView.image = icon
        }else if !wordItem.imgName.isEmpty{
            let img = GameManager.share.getImageBy(wordItem.imgName)
            wordItem.image = img
            iconView.image = img
        }
        numLab.isHidden = true
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        bgImageView.frame = CGRect(x: 10, y: 0, width: self.width-20, height: self.height)
        numLab.frame = self.bounds
        iconView.frame = self.bounds
    }
}
